package com.xinsoft.controller.outsourcing;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.ScmOutsourcingOutstockDTO;
import com.xinsoft.entity.dto.ScmOutsourcingOutstockParam;
import com.xinsoft.entity.vo.ScmOutsourcingOutstockPageVo;
import com.xinsoft.entity.vo.ScmOutsourcingOutstockDetailVo;
import com.xinsoft.service.ScmOutsourcingOutstockService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 委外出库
 *
 * @folder 云供应链/委外管理/委外出库
 */
@RestController
@RequestMapping("/scm/outsourcingOutstock")
public class ScmOutsourcingOutstockController {
    @Autowired
    private ScmOutsourcingOutstockService outsourcingOutstockService;

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody ScmOutsourcingOutstockParam param) {
        return Response.succeed(outsourcingOutstockService.findPageListByParam(param));
    }

    /**
     * 暂存
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody ScmOutsourcingOutstockDTO dto) {
        ValidatorUtils.validateEntity(dto);
        outsourcingOutstockService.tempSave(dto);
        return Response.succeed();
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<ScmOutsourcingOutstockDetailVo> detail(@RequestParam(value = "id") Integer id) {
        return Response.succeed(outsourcingOutstockService.detail(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        ids = ids.stream().distinct().collect(Collectors.toList());
        outsourcingOutstockService.del(ids);
        return Response.succeed();
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") ScmOutsourcingOutstockParam param, HttpServletResponse response) throws Exception {
        List<ScmOutsourcingOutstockPageVo> list = outsourcingOutstockService.findListByParam(param);
        ExcelUtils.exportByEasypoi(list, ScmOutsourcingOutstockPageVo.class, "委外出库", "委外出库", response, "委外出库");
        return Response.succeed();
    }

    /**
     * 审核
     */
    @GetMapping("/audit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> audit(@ApiParam(value = "委外出库单id",required = true) @RequestParam("id") Integer id){
        outsourcingOutstockService.audit(id);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @GetMapping("/reverseAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> reverseAudit(@ApiParam(value = "委外出库单id",required = true) @RequestParam("id") Integer id){
        outsourcingOutstockService.reverseAudit(id);
        return Response.succeed();
    }

    /**
     * 保存并审核
     */
    @PostMapping("/saveAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> saveAudit(@RequestBody ScmOutsourcingOutstockDTO dto){
        ValidatorUtils.validateEntity(dto);
        Integer id = outsourcingOutstockService.tempSave(dto);
        outsourcingOutstockService.audit(id);
        return Response.succeed();
    }
}
